package com.liu.controllor;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;

/**
 * @author liubo
 */
@RestController
@RequestMapping("/consumer/users/")
public class UsersControllor {
    @Autowired
    private RestTemplate restTemplate;

//    private static final String REST_URL_PREFIX = "http://127.0.0.1:8001/provider/users/";
    //上面的指定IP:PORT，如果大流量冲击，此机器承受不了，引进Ribbo进行负载均衡，把请求发到微服务，微服务再用某种负载均衡机制，把请求分散到注册节点上
    private static final String REST_URL_PREFIX = "http://PROVIDER/provider/users/";
//
//    @RequestMapping(value = "addUsers", method = RequestMethod.POST)
//    public Boolean addUsers(@RequestBody JSONObject param) {
//        return restTemplate.postForObject(REST_URL_PREFIX + "addUsers", param, );
//    }
//
//    @RequestMapping(value = "delUsers", method = RequestMethod.DELETE)
//    public void delUsers(@RequestParam(value = "userId") String userId) {
//        restTemplate.delete(REST_URL_PREFIX + "delUsers", userId);
//    }
//
//    @RequestMapping(value = "modifyUsers", method = RequestMethod.PUT)
//    public void modifyUsers(@RequestBody JSONObject param) {
//        restTemplate.put(REST_URL_PREFIX + "modifyUsers", param);
//    }

    @RequestMapping(value = "queryUsersCount", method = RequestMethod.GET)
    public JSONObject queryUsersCount(@RequestParam(value = "usersString") String usersString) {
        return restTemplate.getForObject(REST_URL_PREFIX + "queryUsersCount?usersString=" + URLEncoder.encode(usersString), JSONObject.class);
    }

    @RequestMapping(value = "queryUsers", method = RequestMethod.GET)
    public JSONObject queryUsers(@RequestParam(value = "usersString") String usersString) {
        return restTemplate.getForObject(REST_URL_PREFIX + "queryUsers?usersString=" + URLEncoder.encode(usersString), JSONObject.class);
    }
}
